package com.handinfo.android.game.skill;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.media.DWAudio;
import com.handinfo.android.core.object.Scene;
import com.handinfo.android.game.GameActor;
import com.handinfo.android.game.GameActorConstants;
import com.handinfo.android.game.GameEffect;
import com.handinfo.android.game.Role;
import com.handinfo.android.utils.Tools;

public class CombatSkill
{
	public int m_type; // 0：瞬发 1：被动 2：吟唱 3：绝技
	public int m_targetType = 0; //技能目标类型

	//动作相关
	public int m_singActionId = -1; // 吟唱动作id
	public int m_castSrcActionId = -1; // 施法者动作id

	//光效相关
	public int m_singEffectId = -1; // 吟唱光效
	public int m_castSrcEffectId = -1; // 施法者光效id
	public int m_castProcEffectId = -1; // 施法过程光效id
	public int m_castTgtEffectId = -1; // 施法目标光效id
	public int m_castSpecEffectId = -1; // 特技光效id

	//音效相关
	public int m_singSoundId = -1; // 吟唱音效
	public int m_castSrcSoundId = -1; // 施法者音效id
	public int m_castProcSoundId = -1; // 施法过程音效id
	public int m_castTgtSoundId = -1; // 施法目标音效id
	public int m_castSpecSoundId = -1; // 特技音效id

	public int m_procStartX = -1;
	public int m_procStartY = -1;
	public int m_procEndX = -1;
	public int m_procEndY = -1;

	public int m_castingTime = 0; // 施法时间

	private int m_curStep = -1; //当前阶段
	private int m_curStepFrameCounter; //当前阶段的帧计数器
	private boolean m_active; //是否处于激活的施法状态

	private GameActor m_caster;
	private GameActor m_target;
	private Scene m_scene;
	public SkillResult[] m_skillResults;

	public synchronized void setCaster(GameActor actor)
	{
		m_caster = actor;
	}

	public synchronized void setTarget(GameActor actor)
	{
		m_target = actor;
	}
	public synchronized void setSkillResult(SkillResult[] skillResults)
	{
		m_skillResults = skillResults;
	}

	public void setProcEffectPosition(int startX, int startY, int endX, int endY)
	{
		m_procStartX = startX;
		m_procStartY = startY;
		m_procEndX = endX;
		m_procEndY = endY;
	}

	public int getCurStep()
	{
		return m_curStep;
	}

	public void logic()
	{
		if (!m_active)
			return;
		if (m_curStepFrameCounter > 0) {
			m_curStepFrameCounter--;
			return;
		}
		switch (m_curStep) {
			case SkillConstants.阶段_吟唱:
//				setToStep(SkillConstants.阶段_施法);
				break;
			case SkillConstants.阶段_施法:
				if (m_castSpecEffectId != -1) {
					setToStep(SkillConstants.阶段_特技);
					break;
				}
				if (m_castProcEffectId != -1) {
					setToStep(SkillConstants.阶段_过程);
				}
				else {
					setToStep(SkillConstants.阶段_目标);
				}
				break;
			case SkillConstants.阶段_过程:
			case SkillConstants.阶段_特技:
				if (m_skillResults != null) {
					setToStep(SkillConstants.阶段_目标);
				}
				break;
			case SkillConstants.阶段_目标:
//				deactive();
				break;
		}
	}

	public boolean isActive()
	{
		return m_active;
	}

	public synchronized void active(Scene scene, int firstStep)
	{
		m_active = true;
		m_scene = scene;
		setToStep(firstStep);
	}

	public synchronized void deactive()
	{
		m_scene = null;
		m_active = false;
		m_curStep = -1;
		m_curStepFrameCounter = 0;

		m_procStartX = -1;
		m_procStartY = -1;
		m_procEndX = -1;
		m_procEndY = -1;

		m_caster = null;
		m_target = null;

		if (m_skillResults != null) {
			for (int i = 0; i < m_skillResults.length; i++) {
				m_skillResults[i] = null;
			}
			m_skillResults = null;
		}
	}

	public synchronized void setToStep(int step)
	{
		m_curStep = step;
		m_curStepFrameCounter = 1;
		try {
			switch (m_curStep) {
				case SkillConstants.阶段_吟唱: {
					if (m_caster == null)
						return;
					//吟唱动作
					if (m_singActionId != -1) {
						if (m_caster instanceof Role && ((Role) m_caster).m_isride) {
							m_caster.setAction((m_singActionId - Role.ACTION_ATTACK1 + Role.ACTION_ATTACK1_RIDE)
									* GameActorConstants.DIRECT_NUM + m_caster.m_direct, 0, 0,
									true, true);
						}
						else {
							m_caster.setAction(m_singActionId * GameActorConstants.DIRECT_NUM + m_caster.m_direct, 0,
									0,
									true, true);
						}
					}
					//吟唱光效
					if (m_singEffectId != -1) {
						int casterTime = m_castingTime * 1000 / m_scene.m_sceneHandler.getFrameInterval();
						if (m_caster == m_scene.m_controlledActor
								|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
							m_scene.addEffect(m_singEffectId, m_caster.m_posX, m_caster.m_posY,
									m_caster.m_posX,
									m_caster.m_posY, casterTime, GameEffect.checkFlipX(m_caster));
						}
						m_curStepFrameCounter = casterTime;
					}
					//吟唱音效
					if (m_caster == m_scene.m_controlledActor
							|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
						DWAudio.getInstance().playSound(m_singSoundId);
					}
					break;
				}
				case SkillConstants.阶段_施法: {
					if (m_caster == null)
						return;
					int actionTime = 0;
					int effectTime = 0;
					
					if (m_castSrcActionId != -1) {
						if (m_caster instanceof Role && ((Role) m_caster).m_isride) {
							m_caster.setAction((m_castSrcActionId - Role.ACTION_ATTACK1 + Role.ACTION_ATTACK1_RIDE)
									* GameActorConstants.DIRECT_NUM + m_caster.m_direct, 0, 0,
									false, true);
						}
						else {
							m_caster.setAction(m_castSrcActionId * GameActorConstants.DIRECT_NUM + m_caster.m_direct,
									0, 0,
									false, true);
						}
						actionTime = m_caster.getCurActionTime();
					}
					else {
						m_caster.stay(m_caster.m_direct);
					}
					if (m_castSrcEffectId != -1) {
						if (m_caster == m_scene.m_controlledActor
								|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
							GameEffect effect = m_scene.addEffect(m_castSrcEffectId, m_caster.m_posX,
									m_caster.m_posY, m_caster.m_posX,
									m_caster.m_posY, GameEffect.checkFlipX(m_caster));
							effectTime = effect.getFrameAmount(effect.m_actionIDNow);
						}
					}
					m_curStepFrameCounter = Math.max(actionTime, effectTime);
					if (m_caster == m_scene.m_controlledActor
							|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
						DWAudio.getInstance().playSound(m_castSrcSoundId);
					}
					break;
				}
				case SkillConstants.阶段_过程: {
					if (m_caster == null)
						return;
					if (m_castProcEffectId != -1) {
						GameEffect effect = null;
						switch (m_targetType) {
//							case SkillConstants.TARGET_TYPE_T_LINE_ALL:
//							case SkillConstants.TARGET_TYPE_T_LINE_ENEMY:
//								effect = m_caster.m_scene.addEffect(m_castProcEffectId, m_caster.m_posX,
//										m_caster.m_posY, m_target.m_posX,
//										m_target.m_posY, GameEffect.checkFlipX(m_caster));
//								break;
							case SkillConstants.TARGET_TYPE_SELF:
								if (m_caster == m_scene.m_controlledActor
										|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
									effect = m_scene.addEffect(m_castProcEffectId, m_caster.m_posX,
											m_caster.m_posY, m_caster.m_posX,
											m_caster.m_posY, GameEffect.checkFlipX(m_caster));
								}
								break;
							default:
								if (m_caster == m_scene.m_controlledActor
										|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
									effect = m_scene.addEffect(m_castProcEffectId, m_target.m_posX,
											m_target.m_posY, m_target.m_posX,
											m_target.m_posY, GameEffect.checkFlipX(m_caster));
								}
								break;
						}
						if (effect != null) {
							m_curStepFrameCounter = effect.getFrameAmount(effect.m_actionIDNow);
						}
					}
					if (m_caster == m_scene.m_controlledActor
							|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
						DWAudio.getInstance().playSound(m_castProcSoundId);
					}
					break;
				}
				case SkillConstants.阶段_特技: {
					if (m_caster == null)
						return;
					if (m_castSpecEffectId != -1) {
						if (m_caster == m_scene.m_controlledActor
								|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
							GameEffect effect = m_scene.addEffect(m_castSpecEffectId, m_caster.m_posX,
									m_caster.m_posY, m_caster.m_posX,
									m_caster.m_posY, GameEffect.checkFlipX(m_caster));
							m_curStepFrameCounter = effect.getFrameAmount(effect.m_actionIDNow);
						}
					}
					if (m_caster == m_scene.m_controlledActor
							|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
						DWAudio.getInstance().playSound(m_castSpecSoundId);
					}
					break;
				}
				case SkillConstants.阶段_目标: {
					if (m_skillResults != null) {
						GameActor target;
						for (int i = 0; i < m_skillResults.length; i++) {
							if (m_skillResults[i] != null) {
								target = m_scene.getGameActorByID(m_skillResults[i].m_tgtType,
										m_skillResults[i].m_tgtId);
								if (target != null && target.m_needDraw) {
									if (m_castTgtEffectId != -1 && !target.isDead()) {
										if (m_caster == m_scene.m_controlledActor
												|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
											GameEffect effect = m_scene.addEffect(m_castTgtEffectId,
													target.m_posX,
													target.m_posY, target.m_posX,
													target.m_posY, GameEffect.checkFlipX(target));
											m_curStepFrameCounter = effect.getFrameAmount(effect.m_actionIDNow);
										}
									}
									if (m_caster == m_scene.m_controlledActor
											|| target == m_scene.m_controlledActor) {
										if (DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示战斗信息)) {
											m_skillResults[i].show(m_scene, target.m_posX, target.m_posY
												+ target.m_activeBox[1]);
										}
									}
								}
								if (target != null) {
									target.dealDead();
								}
							}
						}
					}
					if (m_caster == m_scene.m_controlledActor
							|| DWGameManager.getSetting_graphics(DWGameManager.设置画面_显示其他玩家特效)) {
						DWAudio.getInstance().playSound(m_castTgtSoundId);
					}
					deactive();
					break;
				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
}
